﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BaHuan.Entities;
using BaHuan.BusinessLayer;
using BaHuan.Common;

namespace BaHuan
{
    public partial class FrmTimPhieuXuatKho : Form
    {
        PhieuXuatKhoManagement PhieuXuatKhoMgt = new PhieuXuatKhoManagement();

        private NhanVien nhanvien;
        public NhanVien NhanVien
        {
            set { this.nhanvien = value; }
            get { return this.nhanvien; }
        }

        public FrmTimPhieuXuatKho()
        {
            InitializeComponent();
        }

        private void btnTimKiemMuaHang_Click(object sender, EventArgs e)
        {
            dgvTimPhieuXuatKho.DataSource = null;
            DateTime tungay = dpTuNgay.Value;
            DateTime denngay = dpDenNgay.Value;

            if (denngay < tungay)
            {
                MessageBox.Show("Thời gian tìm kiếm không hợp lệ, vui lòng kiểm tra lại!");
                return;
            }

            List<PhieuXuatKho> lstPXK = (from pxk in GetAllPhieuXuatKho()
                                         where (pxk.MaSo.ToLower().Contains(txtTimKiem.Text.ToLower()) ||
                                                pxk.NguoiLapPhieu.ToLower().Contains(txtTimKiem.Text.ToLower())) &&
                                                pxk.NgayThang.Date >= tungay.Date && pxk.NgayThang.Date <= denngay.Date
                                         select pxk).ToList();

            if (lstPXK.Count > 0)
            {
                dgvTimPhieuXuatKho.DataSource = lstPXK;
            }

            if (dgvTimPhieuXuatKho.DataSource == null)
            {
                MessageBox.Show("Không tìm thấy kết quả cần tìm", "Thông báo!");
                return;
            }
        }

        private void FrmTimPhieuXuatKho_Load(object sender, EventArgs e)
        {
            dgvTimPhieuXuatKho.AutoGenerateColumns = false;
            dpDenNgay.Text = DateTime.Now.Date.ToShortDateString();
        }

        private void btnThemMoi_Click(object sender, EventArgs e)
        {
            this.Hide();
            FrmPhieuXuatKho pxk = new FrmPhieuXuatKho(nhanvien);
            pxk.ShowDialog();
            this.Show();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        public List<PhieuXuatKho> GetAllPhieuXuatKho()
        {
            ResultObj resultObj = new ResultObj();
            resultObj = PhieuXuatKhoMgt.GetAllPhieuXuatKho();
            List<PhieuXuatKho> listPhieuXuatKho = (List<PhieuXuatKho>) resultObj.Result;
            return listPhieuXuatKho;
        }

        private void dgvTimPhieuXuatKho_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            PhieuXuatKho pxk = (PhieuXuatKho)dgvTimPhieuXuatKho.CurrentRow.DataBoundItem;
            if (pxk != null)
            {
                this.Hide();
                FrmPhieuXuatKho px = new FrmPhieuXuatKho(nhanvien, pxk);
                px.EditButtonClick = 1;
                px.ShowDialog();
                this.Show();
            }
        }

        private void FrmTimPhieuXuatKho_Activated(object sender, EventArgs e)
        {
            dgvTimPhieuXuatKho.DataSource = null;
        }
    }
}
